package com.hmkj.admin.controller.amount;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.entity.export.ExportUtils;
import com.hmkj.core.constant.Constants;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.common.utils.BigDecimalUtils;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.common.utils.ValidateUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.amount.CashModel;
import com.hmkj.core.model.amount.RechargeModel;
import com.hmkj.core.po.amount.Recharge;
import com.hmkj.core.service.amount.RechargeService;
import com.sun.org.apache.regexp.internal.RE;
import com.xiaoleilu.hutool.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 充值记录
 *
 * @author zhaxiaowei
 * @create 2017-06-21 21:34
 **/
@Controller
@RequestMapping(BaseController.BASE_URI)
public class RechargeJsonController extends GenericController<Integer, Recharge> {

    @Resource
    RechargeService rechargeService;

    /**
     * 充值记录列表json数据
     *
     * @param dtGridPager
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("recharge/json/list")
    @RequiresPermissions("sys:recharge:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        DtGrid dtgrid = dataAuthority(dtGridPager, Constants.AG);
        return rechargeService.getDtGridList(dtgrid, RechargeModel.class);
    }

    /**
     * 充值审核
     *
     * @param rechargeModel
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("recharge/json/audit")
    @RequiresPermissions("sys:recharge:audit")
    public ResultEntity doAudit(RechargeModel rechargeModel) throws Exception {
        if(null == rechargeModel || rechargeModel.getId() == null || rechargeModel.getId() <= 0){
            throw new BussinessException("参数错误");
        }
        if(null == rechargeModel.getActualArrival() || BigDecimalUtils.isLessThan(rechargeModel.getActualArrival(), BigDecimal.ZERO)){
            throw new BussinessException("实际到账金额有误");
        }
//        if(!ValidateUtils.isCharOrNumber(rechargeModel.getOutOrderNo())){
//            throw new BussinessException("外部单号格式有误");
//        }
        //获取备注内容的长度
        if (StrUtil.isBlank(rechargeModel.getRemark())) {
            throw new BussinessException("【审核备注】不能为空");
        }
        int length = rechargeModel.getRemark().getBytes().length;
        if (length > 255) {
            throw new BussinessException("【审核备注】输入的内容长度超过容量，请缩短输入的内容");
        }
        //状态校验
        if(null == rechargeModel.getStatus() || (rechargeModel.getStatus().intValue() != Recharge.STATUS.T3.code && rechargeModel.getStatus() != Recharge.STATUS.T4.code)){
            throw new BussinessException("审核状态有误");
        }
        rechargeService.doAudit(rechargeModel);
        return ok();
    }

    /**
     * 列表导出
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "recharge/json/export", method = RequestMethod.POST)
    public ResultEntity export(String dtGridPager, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 执行导出
        ExportUtils.export(request, response, rechargeService.getDtGridListExport(dtGridPager,RechargeModel.class));
        return ok();
    }

    /**
     * 手动入金
     * @param recharge
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("recharge/json/hand")
    @RequiresPermissions("sys:user:offline:recharge")
    public ResultEntity hand(Recharge recharge) throws Exception {
        //校验
      String result = checkRecharge(recharge);
      if(result!=null){
          throw  new BussinessException(result);
      }
        recharge.setAmount(recharge.getAmount().setScale(2, BigDecimal.ROUND_HALF_DOWN));
        rechargeService.handRecharge(recharge);
        return ok();
    }

    private  String checkRecharge(Recharge recharge) {
        String result = null;
        if (recharge.getUserId()==null){
            result = "用户id 不能为空";
        }else if(recharge.getType()==null){
            result = "请选择充值来源";
        }else if(recharge.getOutOrderNo()==null||recharge.getOutOrderNo().equals("")){
            result = "请填写订单号";
        }else if(recharge.getAmount()==null){
            result = "请输入金额";
        }else if(recharge.getRemark()==null||recharge.getRemark().equals("")){
            result = "请填写备注";
        }
        return result;
    }


}
